var app=getApp();
Page({
  data:{
    orderId:'',
    cancelReason:'',
    disableBtn: true,
    cancelReasonNum: 0,
    flag:false
  },
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
  },
  //获取取消原因
  cancelReason:function(event){
    this.setData({
      cancelReason: event.detail.value,
      cancelReasonNum: event.detail.value.length
    })
    if (event.detail.value.length>0){
      this.setData({
        disableBtn: false
      })
    }else{
      this.setData({
        disableBtn: true
      })
    }
  },
  //点击确定
  cancelOrder:function(){
    var _this=this;
    if (_this.data.flag) {
      return;
    } else {
      _this.setData({
        flag: true
      })
      app.request({
        url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/verifyCancel',
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'sessionId': wx.getStorageSync(app.globalData.sessionId)
        },
        data:{
          orderId:_this.data.orderId,
          info:_this.data.cancelReason
        },
        success: function (res) {
          console.log(res)
          if (res.data.result) {
            wx.showToast({
              title: '取消成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function(){
              wx.redirectTo({
                url: '../details/details?orderId=' + _this.data.orderId
              })
            },2500)
            
          }else{
            _this.setData({
              flag: false
            })
            var msg=res.data.msg;
            wx.showModal({
              title: '提示',
              content: msg,
              showCancel:false,
              confirmText:'知道了',
              success: function (res) {
                if (res.confirm) {
                  if (msg.indexOf('金额') >= 0 || msg.indexOf('请求参数') >= 0) {
                    console.log('用户点击确定')
                  } else {
                    wx.redirectTo({
                      url: '../details/details?orderId=' + _this.data.orderId
                    })
                  }
                }
              }
            })
          }
        }
      })
    }
  },
  //点击返回详情页
  goBackDetails:function(){
    var _this = this;
    wx.redirectTo({
      url: '../details/details?orderId=' + _this.data.orderId
    })
  }

})